package org.spring.springboot.controller;

import java.util.List;

import org.spring.springboot.domain.Category;
import org.spring.springboot.domain.Entity;
import org.spring.springboot.domain.Equipment;
import org.spring.springboot.service.CategoryService;
import org.spring.springboot.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class EquipmentRestController {

    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private CategoryService cateService;
    //localhost:8080/api/category?cid=1
    @RequestMapping(value="/api/category",method=RequestMethod.GET)
    public Entity findByCid(@RequestParam(value="cid",required=true)int cid) {
    	return cateService.findByCid(cid);
    }
    @RequestMapping(value="/api/category/n",method=RequestMethod.GET)
    public Entity findByName(@RequestParam(value="name",required=true)String name) {
    	return cateService.findByName(name);
    }
    @RequestMapping(value="/api/category/all",method=RequestMethod.GET)
    public List<Entity> findAllCategory(){
    	return cateService.findAllCategory();
    }
    //http://localhost:8080/api/category/join?id=2
    @RequestMapping(value="/api/category/join",method=RequestMethod.GET)
    public Category findOneByAttr(@RequestParam(value="id",required=true)int id) {
    	return cateService.findByOneAttrId(id);
    }
    //localhost:8080/api/equipment?id=1
    @RequestMapping(value = "/api/equipment", method = RequestMethod.GET)
    public Equipment findOneEquipment(@RequestParam(value = "id", required = true) int id) {
        return equipmentService.findEquipmentById(id);
    }
    //ok
    @RequestMapping(value = "/api/equipment/all", method = RequestMethod.GET)
    public List<Equipment> findAllEquipment() {
        return equipmentService.findAllEquipment();
    }
    //ok
    @RequestMapping(value = "/api/equipment", method = RequestMethod.POST)
    public Long saveEquipment(@RequestBody Equipment equipment) {
        return equipmentService.saveEquipment(equipment);
    }
    //ok
    @RequestMapping(value = "/api/equipment/update", method = RequestMethod.PUT)
    public void modifyEquipment(@RequestBody Equipment equipment) {
         equipmentService.updateEquipment(equipment);
    }
    //ok
    @RequestMapping(value = "/api/equipment/del/{id}", method = RequestMethod.DELETE)
    public void modifyEquipment(@PathVariable("id")int id) {
    	equipmentService.deleteEquipmentById(id);
    }
    
}
